//
//  StatusViewModel.swift
//  JIMWeiBo
//
//  Created by 陈恒均 on 2018/2/12.
//  Copyright © 2018年 陈恒均. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    
    
    var status : Status?
    
    // MARK:- 对数据处理的属性
    var sourceText : String?
    var createAtText : String?
    
    // MARK:- 对用户数据处理
    var verifiedImage : UIImage?
    var vipImage : UIImage?
    
    // MARK:- 处理用户头像的地址
    var profileURL : NSURL?
    
    var picURLs : [NSURL] = [NSURL]()
    
    var cellHeight : CGFloat = 0
    
    
    init(status : Status) {
        self.status = status
        
        //对微博的创建时间进行处理
        guard let created_at = status.created_at else {
            return
        }
        createAtText = NSDate.createDateString(createAtStr: created_at)
        
        //对来源的字符串进行处理
        guard let source = status.source , source != "" else {
            return
        }
        
        let startIndex = (source as NSString).range(of: ">").location + 1
        let length = (source as NSString).range(of: "</").location - startIndex
        
        sourceText = (source as NSString).substring(with:NSMakeRange(startIndex, length))
        
        

        
        // MARK:- 对用户数据处理
        //对认证图片进行处理
        let verified_type = status.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        //对用户的会员图片进行处理
        
        let mbrank = status.user?.mbrank ?? 0
        
        if mbrank > 0 && mbrank <= 6 {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        // 用户头像的处理
        let profileURLString = status.user?.profile_image_url ?? ""
        profileURL = NSURL(string: profileURLString)
        
        //处理配图数据
        
        let picURLDicts = status.pic_urls?.count != 0 ?status.pic_urls : status.retweeted_status?.pic_urls
        
        if let picURLDicts = picURLDicts{
            for picURLDict in picURLDicts{
                guard let picURLString = picURLDict["thumbnail_pic"] else{
                    continue
                }
                picURLs.append(NSURL(string: picURLString)!)
            }
        }
    }
    
}
